//
//  ViewController.m
//  UI10_UITabView
//
//  Created by LGC on 16/3/22.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TabkeViewOfContact.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

// 联系人数组.
@property (nonatomic, retain) NSArray *arrayOfContact;

@end

@implementation ViewController


- (void)dealloc {
    [_arrayOfContact release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"TableView";
    [self handleData];
    [self createTableView];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - Data 
- (void)handleData {
    NSMutableArray *array = [NSMutableArray array];
    
    
    for (int i = 0; i < 7; i++) {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        NSString *name = [NSString stringWithFormat:@"zhang%d", i];
        [dictionary setObject:name forKey:@"name"];
        
        NSString *nameOfImage = [NSString stringWithFormat:@"h%d.jpeg", i + 1];
        UIImage *image = [UIImage imageNamed:nameOfImage];
        [dictionary setObject:image forKey:@"image"];
        
        NSString *phone = [NSString stringWithFormat:@"%d%d%d", i, i + 1, i + 2];
        [dictionary setObject:phone forKey:@"phone"];
        
        [array addObject:dictionary];
    }
    self.arrayOfContact = array;
   
}




#pragma mark - 知识点1:UITabView 
- (void)createTableView {
    
    // 普通风格的tableView.
    TabkeViewOfContact *tableView = [[TabkeViewOfContact alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    [self.view addSubview:tableView];
    
    [tableView release];
    
    // 指定代理人.
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.separatorColor = [UIColor redColor];
}


#pragma mark - 知识点2: TableViewDataSource 相关协议方法.

/** 必须实现协议方法:1/2 */
/** 分区的行的数量. */
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.arrayOfContact.count;
    
}

/** 必须实现协议方法2/2 */
/** 每行cell的风格. */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%s", __func__);
    NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
    
    //static 在静态区存储变量, 且只初始化一次, 内存进行了优化.
    static NSString *poo1 = @"pool";
    
    // 从重用池中取cell对象.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:poo1];
    
    // 判断cell是否为nil.
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:poo1];
    }
    
    // cell赋值.
    cell.textLabel.text = [[self.arrayOfContact objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    cell.imageView.image = [[self.arrayOfContact objectAtIndex:indexPath.row] objectForKey:@"image"];
    
    cell.detailTextLabel.text = [[self.arrayOfContact objectAtIndex:indexPath.row] objectForKey:@"phone"];
    
    return cell;
    
/** ***************************************************************************************** */
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
